package com.yc.vote.servlet;

import com.google.gson.Gson;
import com.yc.vote.entity.TpUser;
import com.yc.vote.entity.TpVote;
import com.yc.vote.mapper.TpVoteMapper;
import com.yc.vote.util.MBHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "FindVoteServlet", value = "/FindVoteServlet")
public class FindVoteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try(SqlSession sqlSession = MBHelper.openSession();){
            TpVoteMapper mapper = sqlSession.getMapper(TpVoteMapper.class);
            String id = request.getParameter("id");
            String uid = null;
            TpUser myinfo = (TpUser) request.getSession().getAttribute("myinfo");
            if(myinfo!=null){
                uid = myinfo.getId()+"";
            }
            TpVote votes = mapper.selectById(id,uid);
            String json = new Gson().toJson(votes);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().append(json);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
